<?php

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\View\View;

class PostController extends Controller
{

    /**
     * 列表
     * @return Application|Factory|View
     */
    public function index()
    {
        $posts = Post::orderBy("created_at", "desc")->paginate(10);
        return view('post/index', compact('posts'));
    }

    /**
     * 详情
     * @param Post $post
     * @return Application|Factory|View
     */
    public function show(Post $post)
    {
        return view('post/show', compact('post'));

    }

    /**
     * 创建页面
     * @return Application|Factory|View
     */
    public function create()
    {
        return view("post/create");
    }

    /**
     * 创建逻辑
     * @throws ValidationException
     */
    public function store()
    {

        //验证
        $this->validate(request(),[
            'title'=>'required|string|max:100|min:5',
            'content'=>'required|string|min:10',
        ]);
        //逻辑
        $post = Post::create($this->create(['title','content']));
        //渲染
        return redirect("/posts");
    }

    //更新页
    public function edit()
    {
        return view('post/edit');
    }

    //更新逻辑
    public function update()
    {

    }

    //删除逻辑
    public function delete()
    {

    }
}
